<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Scores extends CI_Controller {

	public $data;
	function __construct()
	{
		parent::__construct();

		$data = new stdclass();

		header("Content-Type: text/html; charset=utf-8");
		$this->load->model('abiturient_model', 'abi');
		$this->load->model('user_model', 'user');
		$this->load->model('scores_model', 'score');
	}

	public function index()
	{
		$this->data->title = "Мои баллы";

		if ($this->user->id())
		{
			$score = $this->score->getList($this->user->id());

			$this->data->title = 'Мои баллы';
			$this->data->content = $this->load->view('scores/list', array('scoreList' => $score), true);;
		}
		else
		{
			$_SESSION['error'][] = 'Раздел "Мои баллы" доступен только зарегистрированым'; 
			redirect("/abiturient");
		}

		$this->load->view('index', $this->data);		
	}

	public function add()
	{
		if ($this->user->id())
		{
			$this->data->addingJS[] = 'abiturient.js';
			
			$disciplines = $this->abi->getAllDisciplines();
			// diepr($disciplines);

			$this->data->title = "Добавить абитуриента";
			$this->data->content = $this->load->view('addDocument4Proff',array('disciplines' => $disciplines), true);
		}
		else
			redirect("/abiturient");

		$this->load->view('index', $this->data);
	}

	public function edit($id)
	{
		$data = new stdclass();
		$abiturient = new stdclass();

		$id = abs(@abs($id));
		$user = $this->user->getUser($this->user->id());

		if ($this->user->id() && ($user->tAccType == 1 || $user->tAccType == 2))
		{
			$abiturient = $this->abi->getAbiturById($id);

			$data->title = "Редактирование абитуриента";
			$data->content = $this->load->view('addAbitur', array('abiturient' => $abiturient), true);
		}
		else
			redirect("/abiturient");

		$this->load->view('index', $data);
	}

	public function save($update = false)
	{
		$data = new stdclass();
		$data =(object) $this->input->post();

		foreach ($data as $key => $value)	
			 $data->$key = htmlspecialchars($value);

		$user = $this->user->getUser($this->user->id());

		if ($this->user->id() && ($user->tAccType == 1 || $user->tAccType == 2))
		{
			$data->dCreate = now();

			if (!$update)
			{
				die(json_encode( $check = $this->abi->updaterDB('abiturient', $data) ) );
				$_SESSION['saveAbitur'] = 'Абитуриент успешно добавлен';
			}
			else
			{
				die(json_encode( $check = $this->abi->updaterDB('abiturient', $data, true) ) );
				$_SESSION['saveAbitur'] = 'Абитуриент успешно отредактирован';
			}
		}

	}

}



/* Location: ./application/controllers/welcome.php */